class Watcher {
  constructor(fn) {
    this.fn = fn;
  }

  stateChange(state) {
    console.log(`${state}...`);
  }
}

class Dep {
  watchers = [];

  constructor(state) {
    this.state = state;
  }

  setState(val) {
    if (val !== this.state) {
      this.state = val;
      this.watchers.forEach((w) => {
        w.fn();
        w.stateChange(this.state);
      });
    }
  }

  getState() {
    return this.state;
  }

  addWat(wat) {
    this.watchers = this.watchers.filter((i) => {
      return i !== wat;
    });
    this.watchers.push(wat);
  }

  cancelWat(wat) {
    this.watchers = this.watchers.filter((i) => {
      return i !== wat;
    });
  }
}

// ------------

class Watcher {
  constructor(fn) {
    this.fn = fn;
  }

  stateChange(state) {
    console.log(`${state}...`);
  }
}

class Dep {
  watcher = [];

  constructor(state) {
    this.state = state;
  }

  setState(val) {
    if (val !== this.state) {
      this.state = val;
      this.watcher.forEach((w) => {
        w.fn();
        w.stateChange(this.state);
      });
    }
  }

  addWat(wat) {
    this.watcher = this.watcher.filter((w) => {
      return w !== wat;
    });
    this.watcher.push(wat);
  }

  cancelWat(wat) {
    this.watcher = this.watcher.filter((w) => {
      return w !== wat;
    });
  }
}

// ------
class Watcher {
  constructor(fn) {
    this.fn = fn;
  }

  stateChange(state) {
    console.log(`${state} is Change!`);
  }
}

class Dep {
  watchers = [];

  constructor(state) {
    this.state = state;
  }

  setState(val) {
    if (val !== this.state) {
      this.state = val;
      this.watchers.forEach((wat) => {
        wat.fn();
        wat.stateChange(this.state);
      });
    }
  }

  getState() {
    return this.state;
  }

  addWat(wat) {
    this.watchers.filter((w) => {
      return wat !== w;
    });

    this.watchers.push(wat);
  }

  removeWat(wat) {
    this.watchers.filter((w) => {
      return wat !== w;
    });
  }
}

class Watcher {
  constructor(fn) {
    this.fn = fn;
  }

  changeState(state) {
    console.log(`${state} is change!`);
  }
}

class Dep {
  watchers = [];

  constructor(state) {
    this.state = state;
  }

  getState() {
    return this.state;
  }

  setState(val) {
    if (val !== this.state) {
      this.state = val;
      for (const wat of this.watchers) {
        wat.fn();
        wat.changeState(this.state);
      }
    }
  }

  addWat(wat) {
    this.watchers.filter((w) => {
      return w !== wat;
    });
    this.watchers.push(wat);
  }

  removeWat(wat) {
    this.watchers.filter((w) => {
      return w !== wat;
    });
  }
}
